﻿// YamlLanguageService.cs
//
// Copyright 2008 by Burt Harris
// Licensed under the Microsoft Public License (Ms-PL)
// See: http://www.codeplex.com/yaml/license
//
using Microsoft.Scripting.Hosting;
using Microsoft.VisualStudio.Package;
using Microsoft.VisualStudio.TextManager.Interop;

namespace YaTools.Yaml.LanguagePackage
{
    /// <summary>
    /// YamlSource file.   One of these is created for each YAML file open in VS.
    /// </summary>
    internal class YamlSource : Source
    {
        private int _lastParsed;

        public YamlSource(YamlLanguageService service, IVsTextLines buffer,
                          ScriptSource ss, YamlColorizer colorizer)
            : base(service, buffer, colorizer)
        {
        }

        internal static YamlSource Create(YamlLanguageService service, IVsTextLines buffer)
        {
            string path = FilePathUtilities.GetFilePath(buffer);
            ScriptSource scriptSource = YamlLanguage.DefaultScriptEngine.CreateScriptSourceFromFile(path);
            var colorizer = new YamlColorizer(service, buffer, scriptSource);
            return new YamlSource(service, buffer, scriptSource, colorizer);
        }

        /// <summary>
        /// Triggers periodic background parsing for error checking after 
        /// changes happen.
        /// </summary>
        public override void OnIdle(bool periodic)
        {
            if (_lastParsed != ChangeCount)
            {
                _lastParsed = ChangeCount;
                BeginParse();
            }
            base.OnIdle(periodic);
        }

        public override CommentInfo GetCommentFormat()
        {
            var result = new CommentInfo();
            result.LineStart = "# ";
            result.UseLineComments = true;
            return result;
        }
    }
}